package com.uiao.stone.controller;

import com.uiao.stone.common.exception.BaseException;
import com.uiao.stone.common.exception.BeanConverterException;
import com.uiao.stone.common.exception.BillException;
import com.uiao.stone.common.exception.ExceptionDemoServiceImpl;
import com.uiao.stone.common.exception.PayException;
import com.uiao.stone.common.exception.PermissionDefineException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * @author uiao
 * @Title: 自定义异常测试类
 * @date 2018/8/615:47
 */
@Controller
@RequestMapping(value = "exception", method = RequestMethod.GET)
public class ExceptionDemoController {

    org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ExceptionDemoController.class);
    @Autowired
    private ExceptionDemoServiceImpl impl;

    @GetMapping("beanConverter")
    public void converter(HttpServletRequest request) {
        try {
            throw new BeanConverterException("bean 转换异常");
        } catch (BeanConverterException e) {
            logger.error(e.getMessage());
        }
    }

    @GetMapping("pay")
    public void pay(HttpServletRequest request) {
        try {
            impl.payTest();
        } catch (PayException e) {
            logger.error(e.getMessage());
        }
    }

    @GetMapping("bill")
    public void bill(HttpServletRequest request) {
        try {
            impl.billTest();
        } catch (BaseException e) {
            if (e instanceof BillException) {
                logger.error(((BillException) e).customException());
            } else if (e instanceof PayException) {
                logger.error(e.getMessage());
            }
        }
    }

    @GetMapping("throws")
    public void throwsEx(HttpServletRequest request) throws PermissionDefineException {
        if (true) {
            throw new PermissionDefineException("你没权限");
        }
    }
}
